package struct.queue.realize;

/**
 * @author: 柒月
 * @date: 2025/5/2 下午4:20
 * @version: 1.0
 */
public class ArrayQueue {

    private final int[] array;
    private int size;
    private int front;
    private int rear;
    private final int capacity;

    public ArrayQueue(int size) {
        array = new int[size];
        capacity = size;
        front = rear = size = 0;
    }

    public boolean isEmpty() {
        return this.size == 0;
    }

    public boolean isFull() {
        return this.size == this.capacity;
    }

    public boolean offer(int value) {
        if (isFull()) {
            return false;
        }
        array[rear] = value;
        rear = rear == capacity - 1 ? 0 : rear + 1;
        size++;
        return true;
    }

    public int poll() {
        if (isEmpty()) {
            return -1;
        }
        int result = array[front];
        front = front == capacity - 1 ? 0 : front + 1;
        size--;
        return result;
    }

    public int peek() {
        if (isEmpty()) {
            return -1;
        }
        return array[front];
    }

    public int size() {
        return size;
    }
}
